package co.edu.unal.satacredito.database.administration.ejb;

import java.net.UnknownHostException;
import java.util.List;
import javax.ejb.Stateless;
import org.bson.types.ObjectId;
import co.edu.unal.satacredito.database.administration.exceptions.DAOException;
import co.edu.unal.satacredito.database.administration.singleton.SingletonDataStore;
import co.edu.unal.satacredito.database.connection.BrandDatastoreFactory;
import co.edu.unal.satacredito.database.dao.BrandMongoDao;
import co.edu.unal.satacredito.database.dao.FileLogMongoDao;
import co.edu.unal.satacredito.database.entity.Brand;
import co.edu.unal.satacredito.database.entity.exceptions.NonexistentEntityException;
import co.edu.unal.satacredito.database.entity.exceptions.PreexistingEntityException;

import com.mongodb.MongoException;

/**
 * Session Bean implementation class MonitorDaoEJB
 */
@Stateless
public class BrandDaoEJB implements BrandDaoEJBLocal, BrandDaoEJBRemote {

	private BrandMongoDao daoB;

	/**
	 * Default constructor.
	 * 
	 * @throws DAOException
	 */
	public BrandDaoEJB() throws DAOException {
		this.daoB = new BrandMongoDao(SingletonDataStore.getInstance()
				.getDataStore());
	}
	
	@Override
	public void createBrand(Brand brand) throws DAOException{
		try {
			daoB.create(brand);
		} catch (PreexistingEntityException e) {
			throw new DAOException(e.getMessage());
		}
	}

	@Override
	public void editBrand(String id, Brand brand) throws DAOException{
		try {
			//daoB.update(new ObjectId(id), brand);
			daoB.delete(new ObjectId(id));
			daoB.create(brand);
		} catch (Exception e) {
			throw new DAOException(e.getMessage());
		}
	}

	@Override
	public void deleteBrand(String id) throws DAOException {
		try {
			daoB.delete(new ObjectId(id));
		} catch (NonexistentEntityException e) {
			throw new DAOException(e.getMessage());
		}
	}
	
	@Override
	public List<Brand> findAllBrands() throws DAOException {
		try {
			List<Brand> list = daoB.findAll();
			return list;
		} catch (Exception e) {
			throw new DAOException(e.getMessage());
		}
	}
	
	@Override
	public Brand findBrandById(String id) throws DAOException {
		try {
			return daoB.findById(new ObjectId(id));
		} catch (Exception e) {
			throw new DAOException(e.getMessage());
		}
	}
	
	@Override
	public Brand findByBrand(String nameBrand) throws DAOException {
		try {
			Brand brand = daoB.findByBrandName(nameBrand);
			return brand;
		} catch (Exception e) {
			throw new DAOException(e.getMessage());
		}
	}
}
